package com.flying.map;

import java.util.Objects;

/**
 * @author liupengfei
 * @ClassName: Person
 * @Time: 2023-09-18 20:38
 * @Version: 1.0
 * @Descition
 */
public class Person implements Comparable<Person>{
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


    @Override
    public boolean equals(Object o) {
        System.out.println(this.hashCode()+"Person里的equals()方法");
        if (this == o) return true;
        if (!(o instanceof Person person)) return false;
        return getAge() == person.getAge() && Objects.equals(getName(), person.getName());
    }


    @Override
    public int hashCode() {
        return Objects.hash(getName(), getAge());
    }

    @Override
    public int compareTo(Person o) {
        if (o==null){
            throw new NullPointerException("比较对象为空！");
        }
        if (this==o){
            return 0;
        }

        return o.age!=this.getAge() ? Integer.compare(o.getAge(),this.getAge()):this.getName().compareTo(o.getName());

    }
}
